import { generateRandomId } from "../utils.js";
import { lazyReportBatch } from "../report.js";
export default function pageChange() {
    // hash history
    let oldUrl = '';
    // 当 URL 的片段标识符（以 # 符号开头和之后的 URL 部分）更改时，将触发 hashchange 事件
    window.addEventListener('hashchange',function(event) {
        const newUrl = event.newURL;
        const reportData = {
            from: oldUrl,
            to:newUrl,
            type:"behavior",
            subType:"hashChange",
            startTime: performance.now(),
            uuid:generateRandomId(),
        }
        console.log('behavior hashchange',event,reportData);

        lazyReportBatch(reportData);
        oldUrl = newUrl;
    }, true);

    let from = '';
    window.addEventListener('popstate',function(event) {
        const to = window.location.href
        const reportData = {
            from,
            to,
            type:"behavior",
            subType:"popstate",
            startTime: performance.now(),
            uuid:generateRandomId(),
        };
        console.log('behavior popstate',event,reportData);
        lazyReportBatch(reportData);
        from = to;
    },true);
    
}